import datetime

import pathway as pw
from datetime import date
from typing import Optional

from application.db.mysql_db.nsfc.NsfcInfoList import NsfcInfoList


class NsfcInfoListSchema(pw.Schema):
    information_id: str
    info_type_id: str
    source_id: str
    province_id: str = pw.column_definition(default_value="")
    info_name: Optional[str]
    apply_code: Optional[str]
    original_link: Optional[str]
    publish_time: Optional[str] = pw.column_definition(default_value="")


# class NsfcInfoListRead(pw.io.python.ConnectorSubject):
#
#     def run(self):
#         select_list = NsfcInfoList.select().dicts()
#         for doc in select_list:
#             doc = self.transform(doc)
#             self.next(**doc)

    def transform(self, transform_data):
        transform_data = {k: v for k, v in transform_data.items()
                          if v is not None and v != {}}
        if transform_data.get('publish_date'):
            # 处理publish_date可能已经是字符串的情况
            if isinstance(transform_data['publish_date'], (date, datetime.datetime)):
                transform_data['publish_date'] = transform_data['publish_date'].strftime("%Y-%m-%d")
        return transform_data
